package com.cruise.game.controller.common;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.controller.BaseController;
import com.cruise.game.service.common.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传控制器
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "文件上传相关接口")
@RestController
@RequestMapping("/api/upload")
public class UploadController extends BaseController {

    @Resource
    private UploadService uploadService;

    /**
     * 上传图片
     */
    @ApiOperation("上传图片")
    @PostMapping("/image")
    public ApiResult<Map<String, String>> uploadImage(
            @ApiParam(value = "图片文件", required = true) @RequestParam("file") MultipartFile file) {
        String url = uploadService.uploadImage(file);
        Map<String, String> result = new HashMap<>();
        result.put("url", url);
        return ApiResult.success("上传成功", result);
    }

    /**
     * 上传头像
     */
    @ApiOperation("上传头像")
    @PostMapping("/avatar")
    public ApiResult<Map<String, String>> uploadAvatar(
            @ApiParam(value = "头像图片", required = true) @RequestParam("file") MultipartFile file) {
        String url = uploadService.uploadAvatar(file);
        Map<String, String> result = new HashMap<>();
        result.put("url", url);
        return ApiResult.success("头像上传成功", result);
    }

    /**
     * 上传商品图片
     */
    @ApiOperation("上传商品图片")
    @PostMapping("/product")
    public ApiResult<Map<String, String>> uploadProductImage(
            @ApiParam(value = "商品图片", required = true) @RequestParam("file") MultipartFile file) {
        String url = uploadService.uploadProductImage(file);
        Map<String, String> result = new HashMap<>();
        result.put("url", url);
        return ApiResult.success("商品图片上传成功", result);
    }

    /**
     * 上传完单图片
     */
    @ApiOperation("上传完单图片")
    @PostMapping("/order-complete")
    public ApiResult<Map<String, String>> uploadOrderCompleteImage(
            @ApiParam(value = "完单图片", required = true) @RequestParam("file") MultipartFile file) {
        String url = uploadService.uploadOrderCompleteImage(file);
        Map<String, String> result = new HashMap<>();
        result.put("url", url);
        return ApiResult.success("完单图片上传成功", result);
    }

    /**
     * 上传文件
     */
    @ApiOperation("上传文件")
    @PostMapping("/file")
    public ApiResult<Map<String, String>> uploadFile(
            @ApiParam(value = "文件", required = true) @RequestParam("file") MultipartFile file,
            @ApiParam(value = "目录", required = false) @RequestParam(value = "dir", required = false) String dir) {
        String url = uploadService.uploadFile(file, dir);
        Map<String, String> result = new HashMap<>();
        result.put("url", url);
        return ApiResult.success("上传成功", result);
    }

    /**
     * 删除文件
     */
    @ApiOperation("删除文件")
    @DeleteMapping("/file")
    public ApiResult<Boolean> deleteFile(
            @ApiParam(value = "文件URL", required = true) @RequestParam("url") String fileUrl) {
        boolean success = uploadService.deleteFile(fileUrl);
        return success ? ApiResult.success("删除成功", true) : ApiResult.fail("删除失败");
    }
}
